package com.xpec.c4.db.cfg;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 *@author GuoQi E-mail: guoqilianling@xpec.com.cn
 *@version 创建时间: 2011-2-25上午10:27:54
 */
public class DBConfig {
	
	private static String configRoot = "";

	/**
	 * 参数文件路径
	 */
	private static String filePath = configRoot + "/DBConfig.properties";

	private static Properties prop = new Properties();
	
	public static final List<DBSplitter> splitters = new ArrayList<DBSplitter>();

	public static final String BLOB_FORMAT_VER_PREFIX ="&";
		
	static {
		splitters.add(new DBSplitter(";",";","&xpecSemicolon"));
		splitters.add(new DBSplitter("@","@","&xpecYard"));
		splitters.add(new DBSplitter("#","#","&xpecPound"));
		splitters.add(new DBSplitter("$","\\$","&xpecDollar"));
		splitters.add(new DBSplitter("%","%","&xpecMod"));
	}

	/**
	 * 初始化DBConfig.properties
	 */
	public static void initProperties(){
		FileInputStream fis;
		try {
			filePath = configRoot + "/DBConfig.properties";
			fis = new FileInputStream(filePath);

			prop.load(fis);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获得扫描的间隔时间
	 * 
	 * @return
	 */
	public static long getScanIntervalTime() {
		return Long.valueOf(prop.getProperty("ScanIntervalTime"));
	}

	/**
	 * 获得PO更新的间隔时间
	 * 
	 * @return
	 */
	public static long getUpdateIntervalTime() {
		return Long.valueOf(prop.getProperty("UpdateIntervalTime"));
	}

	/**
	 * 是否使用应用层缓存
	 * 
	 * @return
	 */
	public static boolean isUseApplicationCache() {
		return Boolean.valueOf(prop.getProperty("UseApplicationCache"));
	}

	/**
	 * 缓存超时参数
	 * 
	 * @return
	 */
	public static long getCacheTimeOut() {
		return Long.valueOf(prop.getProperty("CacheTimeOut"));
	}

	/**
	 * 批量提交的数量
	 * 
	 * @return
	 */
	public static int getBatchSize() {
		return Integer.valueOf(prop.getProperty("BatchSize"));
	}

	/**
	 * 缓存的大小数量
	 * 
	 * @return
	 */
	public static int getCacheSize() {
		return Integer.valueOf(prop.getProperty("CacheSize"));
	}

	/**
	 * 缓存的大小数量
	 * 
	 * @return
	 */
	public static long getInactiveTime() {
		return Long.valueOf(prop.getProperty("InactiveTime"));
	}

	/**
	 * 处理非活跃数据的调度时间
	 * 
	 * @return
	 */
	public static String getInactiveTriggerTime() {
		return prop.getProperty("InactiveTriggerTime");
	}

	/**
	 * 公会自动保存的间隔时间
	 * 
	 * @return
	 */
	public static long getAutoSaveUnionIntervalTime() {
		return Long.valueOf(prop.getProperty("AutoSaveUnionIntervalTime"));
	}

	/**
	 * 邮件检查的间隔时间
	 * 
	 * @return
	 */
	public static long getAutoSaveMailIntervalTime() {
		return Long.valueOf(prop.getProperty("AutoSaveMailIntervalTime"));
	}

	/**
	 * 邮件批量处理的数量
	 * 
	 * @return
	 */
	public static int getMailBatchSize() {
		return Integer.valueOf(prop.getProperty("MailBatchSize"));
	}

	/**
	 * 邮件检查的间隔时间
	 * 
	 * @return
	 */
	public static long getAutoSaveLogIntervalTime() {
		return Long.valueOf(prop.getProperty("AutoSaveLogIntervalTime"));
	}

	/**
	 * 邮件批量处理的数量
	 * 
	 * @return
	 */
	public static int getLogBatchSize() {
		return Integer.valueOf(prop.getProperty("LogBatchSize"));
	}

	/**
	 * LOG的线程池最小数量
	 * 
	 * @return
	 */
	public static int getLogDBThreadMinSize() {
		return Integer.valueOf(prop.getProperty("LogDBThreadMinSize"));
	}

	/**
	 * LOG的线程池最大数量
	 * 
	 * @return
	 */
	public static int getLogDBThreadMaxSize() {
		return Integer.valueOf(prop.getProperty("LogDBThreadMaxSize"));
	}

	/**
	 * LOG的线程空闲时间。单位：秒
	 * 
	 * @return
	 */
	public static int getLogDBThreadKeepAliveTime() {
		return Integer.valueOf(prop.getProperty("LogDBThreadKeepAliveTime"));
	}

	/**
	 * 获得配置文件的参数值
	 * 
	 * @param key
	 *            参数名
	 * @return 参数值
	 */
	public static String getValue(String key) {
		return prop.getProperty(key);
	}

	public static void setConfigRoot(String configRoot) {
		DBConfig.configRoot = configRoot;
	}

	public static String getConfigRoot() {
		return configRoot;
	}
	
	/*
	 * 取出 String 內的封裝格式版本訊息
	 * 
	 * @param InOutStr 封裝的字符串
	 * 
	 * @return 封裝格式版本號 (格式不對會回傳0, 因此版本號請從1開始)
	 */
	public static int getBlobVer(StringBuffer InOutStr, String spilter) {
		int BlobVer = 0;
		int prefixlen = DBConfig.BLOB_FORMAT_VER_PREFIX.length();
		// check prefix
		if (InOutStr.length() > prefixlen
				&& InOutStr.substring(0, prefixlen).equals(
						DBConfig.BLOB_FORMAT_VER_PREFIX)) {

			int nextSpIdx = InOutStr.indexOf(spilter);
			// 找不到 spilter 的話就傳回 0
			if (nextSpIdx < 0) {
				return 0;
			}
			
			String verStr = InOutStr.substring(prefixlen, nextSpIdx);
			InOutStr.delete(0, nextSpIdx + spilter.length());

			try {
				BlobVer = Integer.valueOf(verStr);
			} catch (Exception e) {
				e.printStackTrace();
				return 0;
			}

		}
		return BlobVer;
	}
}
